package com.versiFramework.core
{
	import com.versiFramework.versi;
	import com.versiFramework.core.IInitializeClass;
	import com.versiFramework.core.IVersiClass;
	
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;
	
	[DefaultProperty("classes2Register")]
	public class registerClass 
	{
		private var _classes2Register:Array;
		
		public function set classes2Register(setVal:Array):void
		{
			_classes2Register=setVal;
			if (_classes2Register != null) register();
		}
			
		[Bindable] 
		public function get classes2Register():Array
		{
			return _classes2Register;
		}
		
		public function registerClass(classes2Register:Array=null)
		{
			super();
			this.classes2Register = classes2Register;
		}
		
		private function register():void
		{
			for each (var item:* in classes2Register)
			{
				var name:String;
				
				if (item is IInitializeClass)
				{
					item.onInitializeClass();
				}
				
				if (item is IVersiClass || item is IController)
				{
					if (item.name.length)
					{
						name = item.name;
					}
				}
				else
				{
					var classIntrospection:XML = describeType(item);
					var accessorName:XMLList = classIntrospection.accessor.(@name=='name');
					var variableName:XMLList = classIntrospection.variable.(@name=='name');			
					
					if ((variableName.length() || accessorName.length()) && item.name.length)
					{
						name = item.name;
					}
				}
					
				if (!name || ! name.length)
				{
					name= getQualifiedClassName(item).split(":").pop();
					if (item is IVersiClass) item.name=name;
				}
				
				
				versi.registerClass(item,name)
			}
			
			classes2Register = null;
		}
		
	}
}